package com.astar.controller.systemSetup;


import com.astar.dto.result.DefaultResult;
import com.astar.entity.CustomMenu;
import com.astar.entity.Menu;
import com.astar.service.IMenuService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-01-17
 */
@Controller
@RequestMapping("/menuManage")
public class MenuController {

    @Autowired
    private IMenuService menuServiceImpl;


    @RequestMapping("/index")
    public Object index(Model model){
        //查询所有
        List<Menu> menus = menuServiceImpl.selectAll();
        List<Menu> menuList = new ArrayList<>();
        query(0,menus,menuList);
        model.addAttribute("menuList",menuList);
        return "menuManage/index";
    }



    //递归
    protected List<Menu> query(Integer parentId, List<Menu> menuList, List<Menu> cusList){
        for (Menu menu :menuList){
            if (menu.getParentId().equals(parentId)){
                cusList.add(menu);
                Integer id = menu.getMenuId();
                query(id,menuList,cusList );

            }
        }
        return cusList;
    }




    //查看
    @RequestMapping("/view")
    public Object view(Integer id,Model model){
        Menu menu1 = menuServiceImpl.selectOne(id);
        Menu menu2 = menuServiceImpl.selectOne(menu1.getParentId());
        model.addAttribute("menu1",menu1);
        model.addAttribute("menu2",menu2);
        return "menuManage/view";
    }

    @RequestMapping("/toAdd")
    public String addResource(Integer parentId, Model model) throws Exception {
        Menu menu = menuServiceImpl.selectOne(parentId);
        model.addAttribute("menu",menu);
        return "menuManage/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Menu menu) throws Exception {
        return DefaultResult.buildDataResult(menuServiceImpl.insert(menu));
    }
    @RequestMapping("/tree")
    public Object tree(){
        return "menuManage/tree";
    }


    //显示树
    @RequestMapping("/showTree")
    @ResponseBody
    public Object showTree(Integer id){
        if (id == null){
            List<CustomMenu> menu = menuServiceImpl.selectChile(0);
            return menu;
        }else{
            List<CustomMenu> list = menuServiceImpl.selectChile(id);
            return list;
        }
    }

    //修改界面
    @RequestMapping("/toUpdate")
    public Object update(Integer id,Model model){
        Menu menu1 = menuServiceImpl.getById(id);
        Menu menu2 = menuServiceImpl.getById(menu1.getParentId());
        model.addAttribute("menu1",menu1);
        model.addAttribute("menu2",menu2);
        return "menuManage/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Menu menu) throws Exception {
        return DefaultResult.buildDataResult(menuServiceImpl.update(menu));
    }

    @RequestMapping("/delete")
    public Object delete(Integer id){
        UpdateWrapper<Menu> menuUpdateWrapper = new UpdateWrapper<>();
        menuUpdateWrapper.eq("menu_id",id).or().eq("parent_id",id);
        Menu menu = new Menu();
        menu.setVisible("1");
        menuServiceImpl.update(menu,menuUpdateWrapper);
        return "redirect:/menuManage/index";
    }







}
